<?php
/**
 * @package PWE.modele
 * @author Gmax <gmaxfr78@gmail.com>
 */
class modele_redirection extends modele {
    protected static $_table = 'redirection';
    
    private $_modele = 'publication';
    private $_modele_id = 0;
    private $_lien = '';
    private $_code = '301';
            
    function __construct($lien=''){
        if($lien!==''){
            $this->_lien = $lien;
            $this->_constructObject();
        }
    }
    private function _constructObject(){
        $sqlQuery = 'SELECT * FROM '.self::getTable().' AS t ';
        $sqlQuery .= 'WHERE t.lien="'.$this->_lien.'" LIMIT 1';
        
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_modele = $datas['modele'];
            $this->_modele_id = $datas['modele_id'];
            $this->_code =  $datas['code'];
        }
    }
    public function getModele(){
        return $this->_modele;
    }
    public function getModeleId(){
        return $this->_modele_id;
    }
    public function getLien(){
        return str_replace('/', '', $this->_lien);
    }
    /**
     * renvoi le code de redirection 301|302
     * @return string
     */
    public function getCode(){
        return $this->_code;
    }
    /**
     * renvoi l'objet cible du permalien
     * @return null|modele
     */
    public function getTargetObject(){
        $target = null;
        if($this->getModeleId()>0) eval('$target = new modele_'.$this->getModele().'('.$this->getModeleId().');');
        return $target;
    }
    public function getDatas($input=false){
        $datas = array(
            'modele'=>$this->getModele(),
            'modele_id'=>$this->getModeleId(),
            'lien'=>$this->getLien(),
            'code'=>$this->getCode()
        );
        return $datas;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if(property_exists(get_class($this),$k)){
                $this->$k = $v;
            }
        }
        return true;
    }
    public function create($lien,$modele,$code='301'){
        $datas = array(
            'lien'=>$lien,
            'modele'=>$modele->type,
            'modele_id'=>$modele->getId(),
            'code'=>$code            
        );
        $this->setDatas($datas);
        $this->save();
    }
    public function save(){
        $sqlQuery = 'INSERT INTO '.self::getTable().' ';
        $sqlQuery .= '(modele,modele_id,lien,code) ';
        $sqlQuery .= 'VALUES (';
        $sqlQuery .= '"'.$this->getModele().'",';
        $sqlQuery .= $this->getModeleId().',';
        $sqlQuery .= '"'.$this->getLien().'",';
        $sqlQuery .= '"'.$this->getCode().'"';
        $sqlQuery .= ')';
        bdd_sql::doSql($sqlQuery,false);

        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function update(){
        $sqlQuery = 'UPDATE '.self::getTable().' SET ';
        $sqlQuery .= 'lien="'.$this->getLien().'", ';
        $sqlQuery .= 'code="'.$this->getCode().'" ';
        $sqlQuery .= 'WHERE modele="'.$this->getModele().'" AND modele_id='.$this->getModeleId().' LIMIT 1';
        bdd_sql::doSql($sqlQuery,false);

        return array('success'=>true,'datas'=>$this->getDatas());
    }
    
    public function delete() {
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE lien="'.$this->getLien().'"';
        bdd_sql::doSql($sqlDelete,false);
        return array('success'=>true,'datas'=>array());
    }
    
    public function doRedirection(){
        if($this->getTargetObject()!==null){
            switch($this->getCode()){
                case '301' : { //redirection permanante
                    header('HTTP/1.1 301 Moved Permanently', true, 301);
                    break;
                }
                case '302' : { // redirection temporaire
                    header('HTTP/1.1 302 Found', true, 302);
                    break;
                }
                default :{
                    header('HTTP/1.1 301 Moved Permanently', true, 301);
                    break;
                }
            }
            header('Location: '.$this->getTargetObject()->getUrl());
        }
    }
    
    /* méthodes static */
    
    /**
     * 
     * @param int $modele_id
     * @param string $modele
     * @param int $start
     * @param int $limit
     * @return \modele_redirection
     */
    public static function getListeFromModele($modele_id, $modele, $start=0,$limit=0){
        $sql = 'SELECT * FROM '.self::getTable().' WHERE modele="'.$modele.'" AND modele_id='.$modele_id.' ';
        if($limit>0){
            $sql .= 'LIMIT '.$start.','.$limit;
        }
        $retour = array();
        $result = bdd_sql::doSql($sql,true,true);
        if($result['count']>0){
            foreach($result['results'] as $datas){
                $retour[] = new modele_redirection($datas['lien']);
            }
        }
        return $retour;
    }
    
    /**
     * 
     * @return string
     */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
}

?>
